  var productTable;
  $(function() {
	getTreeNode();

  	$("#treeAdd").click(function() {
        var parentList = new Array();

        var arr = $('#treeview5').treeview('getSelected');

        if (arr.length > 0){
            getparent(arr[0].nodeId, parentList);
			var menu = "";
			var menuId = "";
			for (var i = parentList.length - 1; i >= 0; i--) {
				menu += parentList[i][0] + ">";
				menuId += parentList[i][1] + ",";
			}
			$("#addModal .form input[name='parentId']").val(arr[0].id);
			$("#addModal .form input[name='menu']").val(menu + arr[0].text);
			$("#addModal .form input[name='menuId']").val(menuId + arr[0].id);
        }else {
            $("#addModal .form input[name='parentId']").val("0");

        }

  		// show
  		$('#addModal').modal({
  			backdrop: false,
  			keyboard: false
  		}).modal('show');



  	});
    $("#treeUpdate").click(function() {
        var parentList = new Array();

        var arr = $('#treeview5').treeview('getSelected');
        if (arr.length ==0){
            layer.open({
                title: '系统提示',
                content: ("请选择目录"),
                icon: '2'
            });
            return;
        }
			getparent(arr[0].nodeId, parentList);
			var menu = "";
			var menuId = "";
			for (var i = parentList.length - 1; i >= 0; i--) {
				menu += parentList[i][0] + ">";
				menuId += parentList[i][1] + ",";
			}
          // base data
          $("#updateModal .form input[name='id']").val(arr[0].id);
          $("#updateModal .form input[name='text']").val(arr[0].text);
          $("#updateModal .form input[name='parentId']").val(arr[0].parentId);
          $("#updateModal .form input[name='menu']").val(menu+arr[0].text);
          $("#updateModal .form input[name='menuId']").val(menuId+arr[0].id);

          // show
          $('#updateModal').modal({
              backdrop: false,
              keyboard: false
          }).modal('show');
      });
    $("#treeDel").click(function() {
        layer.confirm('确认删除?', {icon: 3, title:'系统提示'}, function(index){
            layer.close(index);
            var arr = $('#treeview5').treeview('getSelected');
            var data = {
                "id": arr[0].id
            };
            $.ajax({
                type: "post",
                data: data,
                dataType: "json",
                url: base_url+"/product/deleteTreeNode",
                success: function(data) {
                    if (data.code == 200) {

                        layer.open({
                            title: '系统提示',
                            content: "成功",
                            icon: '1',
                            end: function(layero, index){
                                getTreeNode();

                            }
                        });
                    } else {
                        layer.open({
                            title: '系统提示',
                            content: ("失败"),
                            icon: '2'
                        });
                    }
                }
            });

        });



      });
	$("#addModal").on('hide.bs.modal', function () {
		$("#addModal .form")[0].reset();
		
	});
	$('#searchBtn').on('click', function(){
		productTable.fnDraw();
	});
  	productTable = $("#productTable").dataTable({
  		"deferRender": true,
  		"processing": true,
  		"serverSide": true,
  		"aaSorting": [
  			[1, "asc"]
  		],
  		"ajax": {
  			url: base_url+"/product/pageList",
  			type: "post",
  			data: function(d) {
  				var obj={};
  				var obj={
  				    "treeId":$('#id').val(),
  				    "menuId":$('#id').val(),
  					"name"	:$('#name').val(),
  					"storagepath"	:$('#storagepath').val(),
  					"start":d.start,
  					"length":d.length
  				}
  				return obj;
  			}
  		},
  		"searching": false,
  		"ordering": false,
  		// "scrollX": true, // X轴滚动条，取消自适应
  		"fnDrawCallback": function() {
  			this.api().column(0).nodes().each(function(cell, i) {
  				cell.innerHTML = i + 1;
  			});
  		},
  		"columns": [{
  			"data": null
  		},{
  			"data": 'name',
  			"visible": true
  		}, {
  			"data": 'mark',
  			"visible": true
  		}
  		, {
  			"data": 'cycle',
  			"visible": true
  		}
		,
		{
			"data": 'storagepath',
			"visible": true
		}
  		,{
  			"data": '操作',
  			"render": function(data, type, row) {
  				tableData['key' + row.id] = row;
  				return function() {
  					return getbutton(row);
  				};
  			}
  		}

  		],
  		"language": {
  			"sProcessing": "处理中...",
  			"sLengthMenu": "每页 _MENU_ 条记录",
  			"sZeroRecords": "没有匹配结果",
  			"sInfo": "第 _PAGE_ 页 ( 总共 _PAGES_ 页，_TOTAL_ 条记录 )",
  			"sInfoEmpty": "无记录",
  			"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
  			"sInfoPostFix": "",
  			"sSearch": "搜索:",
  			"sUrl": "",
  			"sEmptyTable": "表中数据为空",
  			"sLoadingRecords": "载入中...",
  			"sInfoThousands": ",",
  			"oPaginate": {
  				"sFirst": "首页",
  				"sPrevious": "上页",
  				"sNext": "下页",
  				"sLast": "末页"
  			},
  			"oAria": {
  				"sSortAscending": ": 以升序排列此列",
  				"sSortDescending": ": 以降序排列此列"
  			}
  		}
  	});
  	var tableData = {};
  	$("#reportTable1Submit").click(function() {
    	$.post(base_url + "/product/save",  $("#addModal .form").serialize(), function(data, status) {
            if (data.code == "200") {
                $('#addModal').modal('hide');
                layer.open({
                    title: '系统提示',
                    content: '新增成功',
                    icon: '1',
                    end: function(layero, index){
                        productTable.fnDraw();
                        getTreeNode();
                    }
                });
            } else {
                layer.open({
                    title: '系统提示',
                    content: (data.msg || "新增失败"),
                    icon: '2'
                });
            }


    	});
  
  	});
  	$("#reportTableUpdateSubmit").click(function() {
  		$.post(base_url + "/product/save",  $("#updateModal .form").serialize(), function(data, status) {
            if (data.code == "200") {
                $('#updateModal').modal('hide');
                layer.open({
                    title: '系统提示',
                    content: '更新成功',
                    icon: '1',
                    end: function(layero, index){
                        productTable.fnDraw();
                        getTreeNode();
                    }
                });
            } else {
                layer.open({
                    title: '系统提示',
                    content: (data.msg || "更新失败"),
                    icon: '2'
                });
            }


    	});
  	});
  	$("#productTable").on('click', '.update', function() {
  		var id = $(this).parent('p').attr("id");
  		var row = tableData['key' + id];
  		if (!row) {
  			layer.open({
  				title: '系统提示',
  				content: ("模型信息加载失败，请刷新页面"),
  				icon: '2'
  			});
  			return;
  		}
  		// base data
        $("#updateProductModal .form input[name='id']").val(row.id);
        $("#updateProductModal .form input[name='treeId']").val(row.treeId);
        $("#updateProductModal .form input[name='menu']").val(row.menu);
        $("#updateProductModal .form input[name='menuId']").val(row.menuId);
        $("#updateProductModal .form input[name='name']").val(row.name);
        $("#updateProductModal .form input[name='mark']").val(row.mark);
        $("#updateProductModal .form input[name='cycle']").val(row.cycle);
        $("#updateProductModal .form input[name='mapurl']").val(row.mapurl);
        $("#updateProductModal .form input[name='featurename']").val(row.featurename);
        $("#updateProductModal .form input[name='storagepath']").val(row.storagepath);
        $("#updateProductModal .form input[name='gdbpath']").val(row.gdbpath);
        $("#updateProductModal .form textarea[name='bz']").text(row.bz);


        // show
  		$('#updateProductModal').modal({
  			backdrop: false,
  			keyboard: false
  		}).modal('show');
  	});
  	$("#updateProductModal").on('hide.bs.modal', function () {
		  $("#updateProductModal .form")[0].reset();

	  });
  	$("#productAdd").click(function() {
			  var parentList = new Array();

			  var arr = $('#treeview5').treeview('getSelected');

			  if (arr.length ==0){
				  layer.open({
					  title: '系统提示',
					  content: ("请选择目录"),
					  icon: '2'
				  });
				  return;
			  }
              getparent(arr[0].nodeId, parentList);
              var menu = "";
              var menuId = "";
              for (var i = parentList.length - 1; i >= 0; i--) {
                  menu += parentList[i][0] + ">";
                  menuId += parentList[i][1] + ",";
              }
              $("#addProductModal .form input[name='treeId']").val(arr[0].id);
              $("#addProductModal .form input[name='menu']").val(menu+arr[0].text);
              $("#addProductModal .form input[name='menuId']").val(menuId+arr[0].id);


          // show
          $('#addProductModal').modal({
              backdrop: false,
              keyboard: false
          }).modal('show');


      });
  	$("#addProductModal").on('hide.bs.modal', function () {
          $("#addProductModal .form")[0].reset();

      });
  	$("#saveProductSubmit").click(function() {
          $.post(base_url + "/product/saveProduct",  $("#addProductModal .form").serialize(), function(data, status) {
              if (data.code == "200") {
                  $('#addProductModal').modal('hide');
                  layer.open({
                      title: '系统提示',
                      content: '新增成功',
                      icon: '1',
                      end: function(layero, index){
                          productTable.fnDraw();

                      }
                  });
              } else {
                  layer.open({
                      title: '系统提示',
                      content: (data.msg || "新增失败"),
                      icon: '2'
                  });
              }

          });

      });
  	$("#updateProductSubmit").click(function() {
          $.post(base_url + "/product/saveProduct",  $("#updateProductModal .form").serialize(), function(data, status) {
              if (data.code == "200") {
                  $('#updateProductModal').modal('hide');
                  layer.open({
                      title: '系统提示',
                      content: '更新成功',
                      icon: '1',
                      end: function(layero, index){
                          productTable.fnDraw();

                      }
                  });
              } else {
                  layer.open({
                      title: '系统提示',
                      content: (data.msg || "更新失败"),
                      icon: '2'
                  });
              }

          });

      });

  });

  function del(obj) {
      layer.confirm('确认删除?', {icon: 3, title:'系统提示'}, function(index){
          layer.close(index);
          var id = $(obj).parent('p').attr("id");
          var data = {
              "id": id
          };
          $.ajax({
              type: "post",
              data: data,
              dataType: "json",
              url: base_url+"/product/deleteProduct",
              success: function(data) {
                  if (data.code == 200) {

                      layer.open({
                          title: '系统提示',
                          content: "成功",
                          icon: '1',
                          end: function(layero, index){
                              productTable.fnDraw();

                          }
                      });
                  } else {
                      layer.open({
                          title: '系统提示',
                          content: ("失败"),
                          icon: '2'
                      });
                  }
              }
          });

      });

  }
  
  function getparent(nodeId,parentList){
		var arr = $('#treeview5').treeview("getParent", nodeId);
        var a=[arr.text,arr.id];
		if(arr.nodeId!=undefined){
			parentList.push(a);
			return getparent(arr.nodeId,parentList);

		}else{	
			return parentList;
		}
		
  }
  function getTreeNode(){
	  	$.ajax({
	  		type: "post",
	  		dataType: "json",
	  		url: base_url+"/product/getTreeNode",
	  		success: function(defaultData) {
	  			$('#treeview5').treeview({
	  				color: "#428bca",
	  			    //icon: "glyphicon glyphicon-stop",
	  			    //selectedIcon: "glyphicon glyphicon-stop",
	  				//nodeIcon: 'glyphicon glyphicon glyphicon-stop',

	  				levels: 1,
	  				data: defaultData,
	  				onNodeSelected: function(event, data) {
	  					$('#id').val(data.id);
	  					productTable.fnDraw();
	  					$('#id').val("");
	  				}
	  			});

	  		}
	  	});
	   }